<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>编辑地址</title>
    <link rel="stylesheet" href="css/base.css" />
    <link rel="stylesheet" href="css/all.css" />
    <link rel="stylesheet" href="css/iconfont.css" />
    <link rel="stylesheet" href="css/add-address.css" />
    <link rel="stylesheet" href="./css/address/address.css"/>
</head>
<body>
<div>
    <div class="head">
         <span onclick="go()">
        <i class="iconfont icon-iconfontjiantou1"></i>
         </span>
        <h1 class="titles">编辑地址</h1>
        <a href="javascript:save();">
            <span class="achieve">保存</span>
        </a>
    </div>
    <div class="middles">
        <div class="name">
            <span>收货人</span>
            <input type="text" id = "name" placeholder="请输入收货人姓名" class="resetUpwd-inputs" />
        </div>
        <div class="name">
            <span>联系电话</span>
            <input type="text" id = "mobile"  placeholder="请输入收货人联系电话" class="resetUpwd-inputs" />
        </div>
        <div class="name clearfix">
            <i>所在地区</i>
            <i class="iconfont icon-youjiantou f_right"></i>
            <div id="city" class="address f_right">请选择</div>
        </div>
        <div class="address">
            <span>详细地址</span>
            <textarea class="describes" id = "address" placeholder="请输入详细地址:如道路、门牌号、可精确到街道"></textarea>
        </div>
    </div>
</div>
<script src="js/x_rem.js"></script>
<script src="js/all.js"></script>
<script src="./js/jquery.min.js"></script>
<script src="./js/address/common.js"></script>
<script src="./js/address/popt.js"></script>
<script src="./js/address/cityjson.js"></script>
<script src="./js/address/cityset.js"></script>
<script>
    $(function () {
        /*地区选择*/
        $("#city").click(function (e) {
            SelCity(this,e);
            /*  console.log(this);*/
        });

        var addressId = GetQueryString("addressId");

        $.ajax({
            type: "POST",
            url: "/app/account/getDefaultAddress",
            contentType: "application/json;charset=utf-8",
            data : JSON.stringify({
                "addressId":addressId
            }),
            dataType: "json",
            beforeSend: function(XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("token", token);
                XMLHttpRequest.setRequestHeader("userId", userId);
            },
            error: function(request) {
                alert("Connection error ");
            },
            success: function(data) {
                if (data.message.code == 200) {
                    $("#name").val(data.data.defaultAddress.name);
                    $("#mobile").val(data.data.defaultAddress.mobile);
                    $("#hcity").val(data.data.defaultAddress.province);
                    $("#hproper").val(data.data.defaultAddress.city);
                    $("#harea").val(data.data.defaultAddress.area);
                    $("#city").html(data.data.defaultAddress.pcadetail);
                    $("#address").val(data.data.defaultAddress.address);
                }
            }
        });

    })
    
    // 保存地址
    function save(){
        var addressId = GetQueryString("addressId");
    	var name = $("#name").val();
    	var mobile = $("#mobile").val();
    	var province = $("#hcity").val();
    	var city = $("#hproper").val();
    	var area = $("#harea").val();
    	var pcadetail = $("#city").html();
    	var address = $("#address").val();
    	localStorage.setItem("userId", userId);
		localStorage.setItem("token", token);
		
		$.ajax({
			type: "POST",
			url: "/app/account/saveAddress",
			contentType: "application/json;charset=utf-8",
    		data : JSON.stringify({
                    "id": parseInt(addressId),
		   			"name":name,
		   			"mobile":mobile,
		   			"province":province,
		   			"city":city,
		   			"area":area,
		   			"pcadetail":pcadetail,
		   			"address":address
		    		}),
			dataType: "json",
			beforeSend: function(XMLHttpRequest) {
				XMLHttpRequest.setRequestHeader("token", token);
				XMLHttpRequest.setRequestHeader("userId", userId);
			},
			error: function(request) {
				alert("Connection error ");
			},
			success: function(data) {
				if(data.message.code == 200) {
					//alert(data.data);
                    location.href = './address-administration.html';
                } else {
					console.log(data.message.msg)
					alert(data.message.msg);
				}
			}
		});
    }
</script>
</body>
</html>